package com.bricker.laboratorymanagement.business.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bricker.laboratorymanagement.business.dao.SysGroupMapper;
import com.bricker.laboratorymanagement.business.entity.SysGroup;
import com.bricker.laboratorymanagement.business.entity.SysUser;
import com.bricker.laboratorymanagement.business.model.GroupSelectModel;
import com.bricker.laboratorymanagement.business.service.SysGroupService;
import com.bricker.laboratorymanagement.business.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class SysGroupServiceImpl extends ServiceImpl<SysGroupMapper, SysGroup> implements SysGroupService {

    @Autowired
    private  SysGroupMapper sysGroupMapper;

    private final String AdminUserType = "2";

    @Autowired
    private SysUserService sysUserService;
    @Override
    public Page<SysGroup> getSysGroup(GroupSelectModel groupSelectModel) {
        Page<SysGroup> page =new Page<>(groupSelectModel.getCurrent(),groupSelectModel.getSize());
        return page.setRecords(sysGroupMapper.getSysGroup(page,groupSelectModel));

    }

    @Override
    public Map<String,String> saveOrUpdMySysGroup(Long uId, List<SysGroup> groupList) {
        Map<String,String> map=new HashMap<>();
        if (CollectionUtils.isNotEmpty(groupList)) {
            SysUser sysUser =sysUserService.getById(uId);
            for (SysGroup sysGroup : groupList) {
                //管理员修改小组信息
                if(AdminUserType.equals(sysUser.getUserType())){
                    sysGroupMapper.updateById(sysGroup);
                }else { //普通用户加入小组
                    if(sysUser.getGroupId() > 0 ){
                        map.put("code","0");
                        map.put("message","您已拥有小组");
                    }else {
                        if(sysGroup.getId()==null){
                            sysGroup.setUserIds(uId+"");
                            sysGroup.setCreatedBy(uId);
                            sysGroup.setDelFlag("0");
                            sysGroup.setCreatedTime(LocalDateTime.now());
                            super.save(sysGroup);
                            SysUser user =sysUserService.getById(uId);
                            user.setGroupId(sysGroup.getId());
                            user.setIsLeader("1");
                            user.setUpdateBy(uId);
                            user.setUpdateTime(LocalDateTime.now());
                            sysUserService.updateById(user);
                            map.put("code","1");
                            map.put("message","创建小组成功");
                        }else {
                            sysGroup.setUpdatedBy(uId);
                            sysGroup.setUpdatedTime(LocalDateTime.now());
                            super.updateById(sysGroup);
                            map.put("code","2");
                            map.put("message","更改小组成功");
                        }
                    }
                }
            }
        }
        return  map;

    }

    @Override
    public SysGroup getMySysGroup(Long id) {
        SysGroup sysGroup =sysGroupMapper.getMySysGroup(id);
        String userIds=sysGroup.getUserIds();
        if(StringUtils.isNotEmpty(userIds)){
            List<Long> idList= Arrays.asList(userIds.split(",")).stream().map(x->Long.valueOf(x)).collect(Collectors.toList());
            List<String> userNameList=sysUserService.list(Wrappers.<SysUser>lambdaQuery().in(SysUser::getId,idList))
                    .stream().map(SysUser::getNickName).collect(Collectors.toList());
            String userNames=String.join(",",userNameList);
            sysGroup.setUserNames(userNames);
        }
        return sysGroup;
    }
}
